<?php
namespace app\backend\controller;

use app\backend\model\Department as DepartmentModel;
use app\backend\model\Menu as MenuModel;
use think\Db;
use think\Request;
use app\backend\org\Helper;

class Department extends Base
{

    // 部门列表展示
    public function index()
    {
        $model = new DepartmentModel;
        // 获取分页显示
        $result = $model->getAll()->paginate(5);
        $page = $result->render();
        $this->assign('page',$page);
        $this->assign('department',$result);
        $this->assign('empty',"暂无数据");
       // $this->assign('act','index');//高亮js
        return $this->fetch();

    }

    //新建部门
    public function department_create(){
        if (Request::instance()->isPost())
        {
            $model = new DepartmentModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
            //var_dump($post);exit;
            // die();
            // 判断已有内容不能重复新增去修改
            $name = Db::query("select department_id,department_name_cn from ls_department where `department_name_cn`='".$post['department_name_cn']."' and delete_time is NULL ");
            //var_dump($name);exit;
            if(!empty($name)){
                return $this->redirect(url('department_update',array('id'=>$name[0]['department_id'])));
            }

            // 验证表单并插入数据库
            $result = $this->validate($post,'Department.add');
            if(true !== $result){
                // 验证失败 输出错误信息
                dump($result);
                return $this->error($msg = '参数错误，验证失败');
            }

            // 登录人的ID
            $post["creator_id"] = \think\Session::get('login_id');
            //$model->insert($post);

            // 跳转到显示页面
            // 插入数据库后修改数据库跳转连接
            $line = $model->save($post);
            //var_dump($model);
            //var_dump($line);die();
            if(!$line){
                return $this->error($this->update_error);
            } else {
                return $this->redirect('department/index');
            }
        };

        //查询某个分类的直接子类id和名字
        $this->assign('act','index');
        return $this->fetch();
    }

    //部门修改
    public function department_update($id)
    {
        if (Request::instance()->isPost())
        {
            $model = new DepartmentModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量

            //验证表单并插入数据库
            $result = $this->validate($post,'Department.edit');
            if(!$result){
                $this->error($result->getError());
            }
            // 过滤掉数据表中没有的字段，保存
            $result = $model->allowField(true)->save($post,['department_id'=>$id]);
            //echo $result;exit;
            if(false === $result){
                $this->error($this->update_error);
            } else {
                $this->redirect('department/index');
            }
        }
        //根据主键插记录
        $record = DepartmentModel::get($id);
        //var_dump($record);exit;
        $this->assign('id',$id);
        $this->assign('department', $record);
        $this->assign('act','index');
        return $this->fetch();
    }

    //删除部门  软删除，记录删除时间
    public function department_delete($id)
    {
        DepartmentModel::destroy($id);//软删除
        //AlltimeModel::destroy($id,true);//真实删除

        $this->redirect('department/index');
        return $this->fetch();
    }

    //启用部门
    public function department_enable($id){
        DepartmentModel::update(['is_enable'=>1],['department_id'=>$id]);
        $this->redirect('department/index');
        return $this->fetch();
    }


}